package com.dongdong.service.impl;


import com.dongdong.api.SellerServiceApi;
import com.dongdong.mapper.TbSeckillGoodsMapper;
import com.dongdong.model.TbSeckillGoods;
import com.dongdong.model.TbSeckillGoodsExample;
import com.dongdong.model.TbSeller;
import com.dongdong.service.SeckillGoodsService;
import com.dongdong.vo.SeckillGoodsVO;
import com.dongdong.vo.TbSellerVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class SeckillGoodsServiceImpl implements SeckillGoodsService {

    @Autowired
    private TbSeckillGoodsMapper tbSeckillGoodsMapper;

    @Autowired
    private RedisTemplate redisTemplate;


    @Autowired
    private SellerServiceApi sellerServiceApi;

//预热
    @Override
    public void getRedisHot() {


        Date date = new Date();
        //开始时间   当前时间  结束时间
        TbSeckillGoodsExample example = new TbSeckillGoodsExample();
//        example.createCriteria().andStartTimeLessThanOrEqualTo(date).andEndTimeGreaterThanOrEqualTo(date);
        example.createCriteria()
                .andStartTimeLessThanOrEqualTo(date)
                .andEndTimeGreaterThanOrEqualTo(date);
        List<TbSeckillGoods> tbSeckillGoods = tbSeckillGoodsMapper.selectByExample(example);

        tbSeckillGoods.stream().map((x) -> {
            SeckillGoodsVO vo = new SeckillGoodsVO();
            BeanUtils.copyProperties(x, vo);
            return vo;
        }).forEach((y) -> {
            //redis 存数据
            // 格式 hash
            // 大key--->>seckillGoodsList                    小key---->>商品id  小value---->>一条数据
            //                                               小key---->>商品id 小value---->>一条数据

            redisTemplate.boundHashOps("seckillGoodsList").put(y.getId(), y);
        });


    }

    @Override
    public List<SeckillGoodsVO> findSeckillGoodsList() {
        List<SeckillGoodsVO> seckillGoodsVOS = redisTemplate.boundHashOps("seckillGoodsList").values();
        return seckillGoodsVOS;
    }
//
    @Override
    public SeckillGoodsVO selectSeckillById(Long id) {
        TbSeckillGoods tbSeckillGoods = tbSeckillGoodsMapper.selectByPrimaryKey(id);
        SeckillGoodsVO seckillGoodsVO = new SeckillGoodsVO();
        BeanUtils.copyProperties(tbSeckillGoods, seckillGoodsVO);
        //根据seller_id查询商家信息

        TbSeller seller = sellerServiceApi.selectSellerBySellerId(seckillGoodsVO.getSellerId());
        TbSellerVO sellerVO = new TbSellerVO();
        BeanUtils.copyProperties(seller, sellerVO);
        seckillGoodsVO.setTbSellerVO(sellerVO);
        return seckillGoodsVO;

    }

    @Override
    public SeckillGoodsVO getSeckillGoodsVO(Long id) {

        SeckillGoodsVO seckillGoodsVO = (SeckillGoodsVO)redisTemplate.boundHashOps("seckillGoodsList").get(id);
        return seckillGoodsVO;

    }

    @Override
    public void updateSeckillGoodsNum(SeckillGoodsVO seckillGoodsvo) {
        TbSeckillGoods tbSeckillGoods = new TbSeckillGoods();
        BeanUtils.copyProperties(seckillGoodsvo, tbSeckillGoods);
        tbSeckillGoodsMapper.updateByPrimaryKeySelective(tbSeckillGoods);

    }










}
